package com.example.planetexploder.module.book;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.planetexploder.R;
import com.example.planetexploder.adapter.MyAdapter_ItemInfo;
import com.example.planetexploder.bean.maindata.BookInfo;
import com.example.planetexploder.bean.maindata.ItemInfo;
import com.example.planetexploder.constants.SortConst;
import com.example.planetexploder.module.edittext.View_EditText;
import com.example.planetexploder.module.menu.View_Menu;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;

/**
 * 无底部导航栏的UI设计、菜单显示且监听功能、创建功能
 * 显示摘抄集中的每一条文本或图片摘抄,整体的功能与Main_Activity相似
 * @author lxx
 * 2019.10.19
 */
public class View_Book extends AppCompatActivity implements AdapterView.OnItemLongClickListener{
    private Presenter_Book presenter;
    private BookInfo transmitBookInfo;
    private int sortTag;

    private ArrayList<ItemInfo> mListBook;
    private GridView mGridView;
    private MyAdapter_ItemInfo mAdapter;
    private boolean isShowDelete = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book);
        presenter = new Presenter_Book(View_Book.this);

        //取得启动该Activity的Intent对象
        Intent intent =getIntent();
        //取出Intent中附加的数据
        transmitBookInfo= (BookInfo) intent.getSerializableExtra("BookInfo");

        sortTag = presenter.pSort.get_SortWay();//获取当前排序
        mListBook = presenter.pBook.get_BookTable(transmitBookInfo.bookId, sortTag);//初始化首页数据

        /**
         * 功能：绑定公共类MyGridViewAdapter，实现长按事件的监听并初始化数组
         * 修改日期：2019.10.19
         */
        mGridView = findViewById(R.id.gridViewBook);
        mGridView.setOnItemLongClickListener(this);//长按事件监听
        mAdapter = new MyAdapter_ItemInfo(View_Book.this, mListBook);
        mGridView.setAdapter(mAdapter);


        /**
         * 功能：监控首页右下角加号(+)号按钮的单击行为，调用自定义函数来显示创建窗口
         * 修改日期：2019.10.19
         */
        final FloatingActionButton buttonPlusBook = findViewById(R.id.buttonPlusBook);
        buttonPlusBook.setOnClickListener(new View.OnClickListener() {
            @Override//监听Plus按钮
            public void onClick(View view) {
                plusButtonBook(View_Book.this).show();
            }
        });

        /**
         * 功能：设置GridView中item为单击事件，并根据单击效果进入编辑模式
         * 修改日期：2019.10.19
         */
        GridView gridView = findViewById(R.id.gridViewBook);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                if(mListBook.get(position).isText()) {//跳转到文本编辑界面方法
                    transmitInfo(mListBook.get(position));
                }
                else{
                    Toast.makeText(View_Book.this,"进入图片界面",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 功能：实现对显示页面中的长按删除功能，长按获取位置position，删除信息
     * 修改日期：2019.10.19
     */
    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
        if (isShowDelete) {
            isShowDelete = false;
        } else {
            isShowDelete = true;
            mAdapter.setIsShowDelete(isShowDelete);
            mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                    presenter.pBook.delete_Item(transmitBookInfo,mListBook.get(position).itemId);
                    deleteGridViewItems(position);//删除选中项
                    refreshGridView();//刷新ArrayList数组并重新绑定容器
                }

            });
        }
        mAdapter.setIsShowDelete(isShowDelete);//setIsShowDelete()方法用于传递isShowDelete值
        return true;
    }

    /**
     * 功能：删除选中项方法
     * 修改日期：2019.10.19
     */
    private void deleteGridViewItems(int position) {
        ArrayList<ItemInfo> newList = new ArrayList<ItemInfo>();
        if (isShowDelete) {
            mListBook.remove(position);
            isShowDelete = false;
        }
        newList.addAll(mListBook);
        mListBook.clear();
        mListBook.addAll(newList);

        //重新将items设置为单击事件，实现绑定
        GridView gridView = findViewById(R.id.gridViewBook);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                if(mListBook.get(position).isText()) {//跳转到文本编辑界面方法
                    transmitInfo(mListBook.get(position));
                }
                else{
                    Toast.makeText(View_Book.this,"进入图片界面",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 功能：重写onCreateOptionMenu()方法来添加一个选项菜单
     * 修改日期：2019.10.19
     */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = new MenuInflater(this);
        menuInflater.inflate(R.menu.menu_main,menu);
        return super.onCreateOptionsMenu(menu);
    }

    /**
     * 功能：重写onOptionsItemSelected()方法，指定各个菜单被选择时，做出相应处理
     * 修改日期：2019.10.19
     */
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()){
            case R.id.Edit_Main:
                Intent intent = new Intent(View_Book.this, View_Menu.class);
                startActivity(intent);
                break;

            case R.id.Updata_Book:
                presenter.pSort.set_SortWay(SortConst.ORDER_BY_MOD);
                sortTag = presenter.pSort.get_SortWay();
                refreshGridView();
                break;

            case R.id.CreateDown_Book:
                presenter.pSort.set_SortWay(SortConst.ORDER_BY_CRE_DOWN);
                sortTag = presenter.pSort.get_SortWay();
                refreshGridView();
                break;

            case R.id.CreateUp_Book:
                presenter.pSort.set_SortWay(SortConst.ORDER_BY_CRE_UP);
                sortTag = presenter.pSort.get_SortWay();
                refreshGridView();
                break;
            default:break;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 功能：单击加号跳出选项菜单
     * 修改日期：2019.10.19
     */
    public AlertDialog plusButtonBook(final Context context) {
        AlertDialog builder = new AlertDialog.Builder(context).setView(null)
                .setPositiveButton("新建文本集", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        ItemInfo transInfo = new ItemInfo();//新建一个ItemInfo类并传递
                        transmitInfo(transInfo);
                    }
                }).setNegativeButton("新建图片集", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(View_Book.this,"检测成功",Toast.LENGTH_SHORT).show();
                    }
                }).show();

        //居中显示按钮
        Button btnPositive = builder.getButton(AlertDialog.BUTTON_POSITIVE);
        Button btnNegative = builder.getButton(AlertDialog.BUTTON_NEGATIVE);
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) btnPositive.getLayoutParams();
        layoutParams.weight = 10;
        btnPositive.setLayoutParams(layoutParams);
        btnNegative.setLayoutParams(layoutParams);
        return builder;
    }

    /**
     * 功能：刷新ArrayList数组并重新绑定容器
     * 修改日期：2019.10.21
     */
    public void refreshGridView() {
        mListBook = presenter.pBook.get_BookTable(transmitBookInfo.bookId,sortTag);
        mAdapter = new MyAdapter_ItemInfo(View_Book.this, mListBook);//重新绑定一次adapter
        mGridView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();//刷新GridView
    }

    /**
     *功能：获取首页BookInfo和ItemInfo并进行页面跳转
     * 修改日期：2019.10.21
     */
    public void transmitInfo(ItemInfo itemInfo){
        Intent intent = new Intent(View_Book.this, View_EditText.class);
        intent.putExtra("ItemInfo",itemInfo);//传递Book类和Item类
        intent.putExtra("BookInfo", transmitBookInfo);
        intent.putExtra("Mark",false);
        startActivity(intent);
    }

}